package com.lin.consume;

import com.lin.common.constant.CustomerServiceConstant;
import com.lin.common.error.CustomRuntimeException;
import com.lin.socketio.dto.ChatObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Component
public class ChatsRabbitmqConsumer {
    private static Logger log = LoggerFactory.getLogger(ChatsRabbitmqConsumer.class);

//    @Value("${" + CustomerServiceConstant.socketio_topic + "}")
//    private String socketioTopic;
//
//    @Value("${" + CustomerServiceConstant.content_topic + "}")
//    private String contentTopic;

    /**
     * #{T(com.lin.common.utils.DateUtils).fullDateFormat()} 动态创建队列，是为了每个微服务都能获取到消息，例如TOPIC
     *
     * @param co 消息内容
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue("#{T(com.lin.common.utils.DateUtils).fullDateFormat()}"),  //系统自行 创建临时队列 "#{consumeProperties.getSocketio().get(\"topic\")}" 即每个服务都进行消费
                    exchange = @Exchange(value = "${" + CustomerServiceConstant.socketio_topic + "}", type = CustomerServiceConstant.EXCHANGE_FANOUT)  //绑定的交换机
            )
    })
    public void chatsReceivel(ChatObject co) throws UnsupportedEncodingException, CustomRuntimeException {
        log.info("rabbitmq ChatsConsumer on message group:{} body :{}", co.getCustomerGroupId(), co.toString());
        ConsumerHandler cc = ConsumerHandler.getBean(RocketMqConstant.CHATS_TOPIC + CustomerServiceConstant.CONSUMER_HANDLER);
        cc.onMessage(co);
        log.info("rabbitmq send client success");
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue("contentUpdate"),  //其中一个队列进行消费则可以
                    exchange = @Exchange(value = "${" + CustomerServiceConstant.content_topic + "}")  //绑定的交换机
            )
    })
    public void contentReceivel(ChatObject co) throws UnsupportedEncodingException, CustomRuntimeException {
        log.info("rabbitmq contentReceivel on message body :{}", co.toString());
        ConsumerHandler cc = ConsumerHandler.getBean(RocketMqConstant.UPDATE_GROUP_TOPIC + CustomerServiceConstant.CONSUMER_HANDLER);
        cc.onMessage(co);
    }
}
